'use strict';


export default class extends think.Controller {
	
	// 获取客户信息
	async indexAction () {
		// 这里要获取店家的id
		let id = this.get('id')
		// console.log(id)
		let shopinfo = await this.model('or_department').alias('d').join('`think_or_shopinfo` s on d.id = s.s_id').where({'s.s_id': id}).find()
		this.assign('shopinfo', shopinfo)
		return this.display()
	}


	async getguestinfoAction () {
		let data = this.post()
		let currentTime = Math.round(new Date().valueOf()/1000);
		console.log(data)
		let res = {success: 0, info: ''}
		// 检查改店下是否有该为顾客
		let guest = await this.model('or_guest').where({departmentid: data.shopid, username: data.phone}).find()
		if (!guest.id) {
			res.info = '查无此客户信息！'
		} else {

			// 客户的项目信息
			let guestSubjectInfo = await this.model('or_shop_guestcardsubject')
					.alias('gs')
					.field('gs.*, s.name as sname')
					.join('`think_or_subject` s on s.id=gs.subjectid')
					.where('gs.shopid='+data.shopid+' AND gs.guestid='+guest.id+' AND (gs.endtime>'+currentTime+' OR gs.nownum>0) AND gs.isdisabled=0')
					.select();

			// 客户的产品信息
			let guestGoodsInfo = await this.model('or_shop_guest_temp_reserve')
					.alias('t')
					.field('t.*, g.name as gname')
					.join('`think_or_goods` g on g.id=t.goodsid')
					.where('t.guestid='+guest.id+' AND t.shopid='+data.shopid+' AND t.nownum > 0 AND t.isdisabled=0')
					.select();
			res.success = 1
			res.guest = guest
			res.guestSubjectInfo = guestSubjectInfo
			res.guestGoodsInfo = guestGoodsInfo

		}
		return this.json(res)
	}
}